package org.ala.clickhouse.xml.fields;

/**
 *	字段信息
 *
 *	@author ala
 *	@date 2025年6月9日
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public abstract class CKField<F extends CKField> {

	
	/**
	 * 	列名
	 */
	protected String column;
	/**
	 * 	sql
	 */
	protected String sql;
	/**
	 * 字段上的聚合
	 */
	protected Aggregate aggregate;
	
	public CKField(String column) {
		this.column = column;
		this.sql = column;
	}
	public abstract F copy();
	protected F copyAndAggregate(Aggregate aggregate) {
		F f = copy();
		f.aggregate = aggregate; 
		f.sql = aggregate.toSql(); 
		return f;
	}
	
	/**
	 * 聚合-求最大
	 */
	public F aggregateMax() { return copyAndAggregate(new Aggregate.Max(column)); }
	/**
	 * 聚合-求最小
	 */
	public F aggregateMin() { return copyAndAggregate(new Aggregate.Min(column)); }
	/**
	 * 聚合-求数量
	 */
	public F aggregateCount() { return copyAndAggregate(new Aggregate.Count(column));  }
	/**
	 * 聚合-求去重数量
	 */
	public F aggregateDistinctCount() { return copyAndAggregate(new Aggregate.Count(column)); }
	/**
	 * 聚合-求和
	 */
	public F aggregateSum() { return copyAndAggregate(new Aggregate.Sum(column));  }
	/**
	 * 聚合-求平均
	 */
	public F aggregateAvg() { return copyAndAggregate(new Aggregate.Avg(column)); }
	/**
	 * 聚合-求方差
	 */
	public F aggregateVarPop() { return copyAndAggregate(new Aggregate.VarPop(column)); }
	/**
	 * 聚合-求标准差
	 */
	public F aggregateStddevPop() { return copyAndAggregate(new Aggregate.StddevPop(column)); }
	/**
	 * 聚合-求分位数
	 */
	public F aggregateQuantile(double p) { return copyAndAggregate(new Aggregate.Quantile(p, column));  }
	/**
	 * 聚合-求中位数
	 */
	public F aggregateMedian() { return copyAndAggregate(new Aggregate.Median(column)); }
	/**
	 * 聚合-求位与
	 */
	public F aggregateBitAnd() { return copyAndAggregate(new Aggregate.BitAnd(column)); }
	/**
	 * 聚合-求位或
	 */
	public F aggregateBitOr() { return copyAndAggregate(new Aggregate.BitOr(column)); }
	/**
	 * 聚合-求位异或
	 */
	public F aggregateBitXor() { return copyAndAggregate(new Aggregate.BitXor(column)); }


	/**
	 * 	拿sql
	 */
	public String getSql() {
		return sql;
	}
	/**
	 * 	字段名
	 */
	public String getColumn() {
		return column;
	}
}
